from django.shortcuts import render

# Create your views here.


from django.views.decorators.csrf import csrf_exempt
from django.http import  JsonResponse
from api.utils import success_response_with_data, failed_response_with_data, list_response

import json
import random
from api.models import Member

from api.mylogger import logger

@csrf_exempt
def pingpong(request):
    res = {"code": 200, "msg": "Pong!"}
    return JsonResponse(res, safe=False)


# 发送短信接口
# @csrf_exempt
# def sms_send(request):
#     if request.method == 'POST':
#         obj = json.loads(request.body)
#         logger.info('obj is: {}'.format(obj))
#         phone = obj.get('mobileno', '18013959507')
#         # 生成验证码.
#         code = ''
#         for i in range(6):
#             a = random.choice(range(10))
#             code += str(a)
#         # 短信验证
#         client = AcsClient('LTAI5tAjDcCQ1xPpM5caimZL', 'lpNNBXwhxev8uPhm0zGYMBScLRVbgC')   # 用户管理中的AccessKey管理中可以查看到AccessKey ID 和AccessKey secret
#
#         request = CommonRequest()
#         request.set_accept_format('json')
#         request.set_domain('dysmsapi.aliyuncs.com')
#         request.set_method('POST')
#         request.set_protocol_type('https')  # https | http
#         request.set_version('2017-05-25')
#         request.set_action_name('SendSms')
#
#         request.add_query_param('RegionId', "cn-hangzhou")
#         request.add_query_param('PhoneNumbers', phone)
#         request.add_query_param('SignName', "神笔软件")   # 注意必须是审核通过的
#         request.add_query_param('TemplateCode', "SMS_231440529")      # 注意必须是审核通过的
#
#         request.add_query_param('TemplateParam', "{\"code\":\"%s\"}" % code)    # 你的验证码
#         # request.add_query_param('TemplateParam', "验证码为：${code}，您正在登录，若非本人操作，请勿泄露。" % code)    # 你的验证码
#
#         response = client.do_action(request)
#         logger.info(response)
#         user_name = phone
#         authcode = code
#         print("authcode is", authcode)
#         # result = Member.objects.filter(user_name=user_name).update(authcode=authcode)
#         # logger.info(result)
#
#         return JsonResponse({'code': 200, 'status': 0,  'message': str('验证码'), 'data': {'sms_code': code}})
